package com.zhhs.galaxyott.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.beans.MovieDetailRecBean;
import com.zhhs.galaxyott.utils.Logs;

public class MovieDetailAdapter extends BaseAdapter implements OnClickListener {

    private List<MovieDetailRecBean> ListData;
    private LayoutInflater inflater;
    private Context context;

    public MovieDetailAdapter(Context context, List<MovieDetailRecBean> ListData) {
        this.inflater = LayoutInflater.from(context);
        this.ListData = ListData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return ListData.size();
    }

    @Override
    public Object getItem(int position) {
        return ListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View contentView, ViewGroup parent) {
        ViewHolder holder = null;
        if (contentView == null) {
            holder = new ViewHolder();
            contentView = inflater.inflate(R.layout.item_grid, null);
            holder.tv_title = (TextView) contentView.findViewById(R.id.tv_title);
            holder.tiv_icon = (ImageView) contentView.findViewById(R.id.tiv_icon);
            contentView.setTag(holder);
        } else {
            holder = (ViewHolder) contentView.getTag();
        }

        MovieDetailRecBean bean = ListData.get(position);
        holder.tv_title.setText(bean.getName());

        holder.tiv_icon.setTag(R.id.tiv_icon, bean.getId());
        Glide.with(context).load(bean.getImgurl()).placeholder(R.drawable.movie_detail_rec_def).crossFade()
                .into(holder.tiv_icon);
        holder.tiv_icon.setOnClickListener(this);

        // holder.tiv_icon.configImageUrl(app.imageUrl);

        holder.tiv_icon.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Logs.logD("DEBUG_lc", "THIS IS onFocusChange");
                    ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(true);
                } else {
                    ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(false);
                }

            }
        });

        return contentView;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(context, MovieDetailActivity.class);
        Logs.logD("DEBUG_AD", "THIS IS -->POSITION:" + v.getTag(R.id.tiv_icon));

        intent.putExtra("movieid", (Integer) v.getTag(R.id.tiv_icon));
        context.startActivity(intent);

    }

    static class ViewHolder {
        TextView tv_title;
        ImageView tiv_icon;
    }

}
